package com.checker.servlet;


import com.checker.Utils.MapUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Map;
import java.util.function.Function;

public class HelloHttpServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("===HelloHttpServlet===");
        System.out.println("Method:doGet");
        handleContextConfig();
        System.out.println("=========");
        handleContextContext();
        System.out.println("=========");
        handleHttpServletRequest(req);
        System.out.println("=========");
        handleHttpServletResponse(resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("===HelloHttpServlet===");
        System.out.println("Method:doPost");
        handleContextConfig();
        System.out.println("=========");
        handleContextContext();
        System.out.println("=========");
        handleHttpServletRequest(req);
        System.out.println("=========");
        handleHttpServletResponse(resp);
    }

    private void printServletConfigInitParameter(ServletConfig servletConfig) {
        final Enumeration enumeration = servletConfig.getInitParameterNames();
        if (enumeration == null) {
            return;
        }
        handleEnumeration(enumeration, servletConfig::getInitParameter);
    }


    private void handleEnumeration(Enumeration enumeration, Function<String, Object> valueFunction) {
        while (enumeration.hasMoreElements()) {
            final Object object = enumeration.nextElement();
            final String key = String.valueOf(object);
            final Object value = valueFunction.apply(key);
            if (!"org.apache.tomcat.util.scan.MergedWebXml".equals(key)) {
                System.out.println("element key in enumeration-->" + key + ",value-->" + value);
            }
        }
    }


    private void printServletContextAttribute(ServletContext servletContext) {

        final Enumeration enumeration = servletContext.getAttributeNames();
        if (enumeration == null) {
            return;
        }
        handleEnumeration(enumeration, servletContext::getAttribute);
    }

    protected void handleContextConfig() {
        ServletConfig servletConfig = getServletConfig();
        System.out.println("ServletConfig对象：" + servletConfig);
        System.out.println("Servlet别名：" + servletConfig.getServletName());
        System.out.println("InitParameter in ServletConfig-->:");
        printServletConfigInitParameter(servletConfig);
    }

    protected void handleContextContext() {
        final ServletContext servletContext = getServletContext();
        System.out.println("ServletContext对象：" + servletContext);
        System.out.println("Attribute in ServletContext-->:");
        printServletContextAttribute(servletContext);

    }

    protected void handleHttpServletRequest(HttpServletRequest req) throws IOException {
        final Map parameterMap = req.getParameterMap();

        if (parameterMap != null && !parameterMap.isEmpty()) {
            System.out.println("请求参数为:");
            MapUtils.prettyPrint(parameterMap);
        }
        System.out.println("queryString: " + req.getQueryString());

        final ServletInputStream inputStream = req.getInputStream();
        if (inputStream != null) {
            System.out.println("读取输入流-->" + IOUtils.toString(inputStream, Charset.defaultCharset()));
        }
    }

    protected void handleHttpServletResponse(HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        out.println("My Servlet!");
    }
}
